#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include "Dictionary.hpp"

class Client_Data
{
public:
    Client_Data(const uint16_t client_port, const std::string client_ip  , const int sockfd)
    :client_port_(client_port) , client_ip_(client_ip) , c_sockfd_(sockfd)
    {}

public:
    uint16_t client_port_;
    std::string client_ip_;
    int c_sockfd_;
};


class Task
{
public:
    Task(Client_Data* cd)
        :cd_(cd)
    {}

    // void operator()()
    // {
    //     int fd = cd_->c_sockfd_;
    //     char buffer[1024];
    //     int n = read(fd, buffer, sizeof(buffer));
    //     if (n > 0)
    //     {
    //         buffer[n] = 0;
    //         std::cout << "get a message : " << buffer << std::endl;
    //     }
    //     // 发送回去
    //     write(fd, buffer, sizeof(buffer));
    //     close(fd);
    //     delete cd_;
    // }

    void operator()()
    {
        int fd = cd_->c_sockfd_;
        char buffer[1024];
        int n = read(fd, buffer, sizeof(buffer));
        if (n > 0)
        {
            buffer[n] = 0;
            std::cout << " : get a word " << buffer << std::endl;
            std::string word = buffer;
            std::string meaning = dict_.translate(word);
            // 发送回去
            write(fd, meaning.c_str(), sizeof(meaning.size()));
        }
        close(fd);
        delete cd_;
    }



private:
    Client_Data *cd_;
    Dict dict_;
};
